import { Component, OnInit, Input } from '@angular/core';
import { SNode, CONDITION_WIDTH, CONDITION_GAP, PARENT_CHILD_DISTANCE } from 'src/app/module';
import { SvgService } from 'src/app/svgdom/svg.service';

@Component({
  selector: '[svgline]',
  templateUrl: './svgline.component.html',
  styleUrls: ['./svgline.component.scss']
})
export class SvglineComponent implements OnInit {
  @Input() dataSourcs: SNode;

  // 根据叶子节点的数量获取svg的高度
  GetSvgHeight(): number {
    const height = this.dataSourcs.GetLogicHeight() * (CONDITION_WIDTH + CONDITION_GAP) - CONDITION_GAP;
    return height;
  }
  // 获取path的运行轨迹
  GetPathParticle(): string {
    const path = 'M32 2 h-9 v' + (this.GetSvgHeight() - 3) + ' h10';
    return path;
  }

  // 获取圆图形的位置
  GetCircleYPosition(): number {
    const dis = this.GetSvgHeight() / 2;
    return dis;
  }
  // 收缩节点或者展开节点
  HideOrShow() {
    if (this.dataSourcs.Children.length > 0) {
      if (this.dataSourcs.Data.IsExistele) {
        //  判断该点是否已经收缩
        this.ser.HidePoint.forEach(v => {
          if (v === this.dataSourcs) {
            const ix = this.ser.HidePoint.indexOf(this.dataSourcs);
            this.ser.HidePoint.splice(ix, 1);
          }
        });
        this.showPointDatas(this.dataSourcs);
        this.dataSourcs.Data.IsExistele = false;
      } else {
        this.ser.HidePoint.push(this.dataSourcs);
        this.HidePointDatas(this.ser.HidePoint);
        this.dataSourcs.Data.IsExistele = true;
      }
    }
  }
  // 将有隐藏效果的节点显示
  showPointDatas(point: SNode) {
    this.changeHideOrShow(point, true);
    this.HidePointDatas(this.ser.HidePoint); // 这里再调用一次是应为可能是包含关系的隐藏节点，保留内部的隐藏效果，因为实现的技术原理是将有隐藏效果的点保存在数组中
  }

  // 将有隐藏效果的节点隐藏
  HidePointDatas(HidePoint: SNode[]) {
    if (HidePoint.length > 0) {
      HidePoint.forEach(v => {
        this.changeHideOrShow(v, false);
        v.Data.IsDisplay = true; // 使自身显示,与子节点的线条不显示，点击的那个点不需要隐藏
      });
    }
  }

  // 循环访问所有节点，这里包括自身，所以在调用后，需要把自身的显示重新设置一次
  changeHideOrShow(point: SNode, isplay: boolean) {
    point.Data.IsDisplay = isplay;
    point.Data.IsSvgDisplay = isplay;
    if (point.Children) {
      point.Children.forEach(v => {
        this.changeHideOrShow(v, isplay);
      });
    }
  }

  constructor(private ser: SvgService) {}

  ngOnInit() {}
}
